package com.baixiaowen.xiaoaointerview.Java编程功底篇.手写栈_队列相关算法;

/**
 *
 * 使用2个栈实现一个队列
 *
 * @author Baixiaowen
 */
public class Queue2<T> {

    Stack<T> s1 = new Stack<>();
    Stack<T> s2 = new Stack<>();

    public void enqueue(T e) {
        s1.push(e);
    }

    public T dequeue() {
        if (s2.size() > 0) {
            return s2.pop();
        }

        while (s1.size() > 0) {
            s2.push(s1.pop());
        }
        return s2.pop();
    }

    public int size() {
        return s1.size() + s2.size();
    }

    public static void main(String[] args) {
        Queue2<Integer> queue = new Queue2<>();

        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        queue.enqueue(4);

        for (int i = 0; i < queue.size(); i++) {
            if (i + 1 != queue.dequeue()) {
                System.err.println("成功");
            }
        }
    }

}
